package com.zhuchaooasystem.ui.home.fragment.message;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.ServiceMessageReadHolder;
import com.zhuchaooasystem.base.BaseFragment;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.ServiceMessageEntity;
import com.zhuchaooasystem.presenter.home.fragment.message.ServiceMessageReadContract;
import com.zhuchaooasystem.presenter.home.fragment.message.ServiceMessageReadPresenter;
import com.zhuchaooasystem.utils.ListUtils;
import com.zhuchaooasystem.utils.NetWorkUtil;
import com.zhuchaooasystem.utils.ScreenUtils;

import java.util.List;

import butterknife.BindView;


/**
 * Created by zhangyajiang on 2017/1/9.
 */

public class ServiceMessageReadFragment extends BaseFragment<ServiceMessageReadPresenter>
        implements ServiceMessageReadContract.View, SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnMoreListener {

    @BindView(R.id.frag_service_message_read_recycle)
    EasyRecyclerView recycle;
    //----
    private List<ServiceMessageEntity.ResultBean.SuccessfulBean.ResopnseMegBean> messageList;
    private RecyclerArrayAdapter adapter;
    //----
    private int index;

    @Override
    protected ServiceMessageReadPresenter getPresenter() {
        return new ServiceMessageReadPresenter(mContext, this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_service_message_read;
    }

    @Override
    protected void initVariables() {
        index = 1;
    }

    @Override
    protected void initView() {
        recycle.setRefreshingColor(Key.RECYCLEVIEW_REFRESH_COLORS);
        recycle.setLayoutManager(new LinearLayoutManager(mContext));
        DividerDecoration itemDecoration = new DividerDecoration(getResources().getColor(R.color.color_e1e1e1), ScreenUtils.dip2px(mContext, 1f), 0, 0);
        itemDecoration.setDrawLastItem(true);
        recycle.addItemDecoration(itemDecoration);
        recycle.setAdapter(adapter = new RecyclerArrayAdapter<ServiceMessageEntity.ResultBean.SuccessfulBean>(mContext) {

            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ServiceMessageReadHolder(parent);
            }
        });
        recycle.setEmptyView(R.layout.view_empty);
        recycle.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });
    }

    @Override
    protected void initDataAndLoadData() {
        mPresenter.getMessage(index);
    }


    @Override
    public void refreshCount(int count) {
        ((MessageReadActivity) getActivity()).changeTitle("service", count);
    }

    @Override
    public void refreshData(List<ServiceMessageEntity.ResultBean.SuccessfulBean.ResopnseMegBean> entity) {
        this.messageList = entity;
        //----
        adapter.clear();
        adapter.addAll(entity);
    }

    @Override
    public void loadMoreData(List<ServiceMessageEntity.ResultBean.SuccessfulBean.ResopnseMegBean> data) {
        this.messageList = ListUtils.listPlus(messageList, data);
        adapter.addAll(data);
    }


    @Override
    public void onRefresh() {
        //刷新
        if (!NetWorkUtil.isNetConnected(mContext)) {
            adapter.pauseMore();
            return;
        }
        index = 1;
        mPresenter.getMessage(index);
    }

    @Override
    public void onMoreShow() {
        index++;
        mPresenter.getMessage(index);
    }

    @Override
    public void onMoreClick() {
        mPresenter.getMessage(index);
    }
}
